﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace PartsRetrospect.Model
{
    /// <summary>
    /// UI数据绑定基类,与UI交互模型均继承该类
    /// </summary>
    public abstract class BindingBase : INotifyPropertyChanged
    {
        protected void SetValue<T>(ref T old, T value,
            [CallerMemberName] string propertyName = null)
        {
            if ((old == null && value != null) || (old != null && !old.Equals(value)))
            {
                old = value;
                if (Application.OpenForms.Count == 0) return;
                Application.OpenForms[0]?.Invoke(new Action(() =>
                {
                    if(PropertyChanged!=null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
                }));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public delegate bool ShowMessageDelete(string message);
        /// <summary>
        /// 消息通知
        /// </summary>
        public ShowMessageDelete ShowMessageBox { set; get; }
    }
}
